/*
 * @Copyright: 2017 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.controller.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.pay.common.enums.PayRspCodeEnum;
import com.yyfax.pay.common.enums.PaymentEnum;
import com.yyfax.pay.order.service.AdapterService;
import com.yyfax.pay.order.service.PayService;
import com.yyfax.pay.third.model.to.yyfax.OrderNotifyContent;
import com.yyfax.pay.third.service.ThirdPayService;

/**
 * 支付结果通知 --> 异步通知
 * 
 * @author buyi
 * @date 2017-04-12 20:23:19
 * @since v1.0.0
 */
@RestController
public class PayResultNotifyController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private AdapterService adapterService;

	@Autowired
	private PayService payService;

	/**
	 * 支付异步响应回调入口
	 * 
	 * @author buyi
	 * @date 2017-04-12 21:40:18
	 * @since v1.0.0
	 * @param payment
	 * @param request
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping(value = "/api/result/pay/notify/{paymentCode}")
	public String orderNotify(@PathVariable("paymentCode") String paymentCode, HttpServletRequest request,HttpServletResponse response) throws Throwable {
		PaymentEnum paymentEnum = PaymentEnum.fromValue(paymentCode);
		if (paymentCode == null) {
			return null;
		}

		ThirdPayService adapter = adapterService.adapterPayService(paymentEnum);

		// 1.确认签名,确认订单;
		OrderNotifyContent notifyContent = adapter.transformPayOrderNotify(request);

		if (notifyContent == null || PayRspCodeEnum.PAY_CODE_CHECK_SIGN_FAIL.isValue(notifyContent.getResultCode())) { // 如果验签失败
			logger.info("支付通道：{}异步响应异常/签名失败", paymentCode);
			return null;
		}

		// // 2.获取订单信息
		// PayBusiIdVO payOrderIdVO = payOrderService.resolverPayOrderId(notifyContent.getPayOrderId());
		// PayOrderVO payOrderVO = payOrderService.queryPayOrderVO(notifyContent.getPayOrderId(), payOrderIdVO.getMerchantCode());
		// if (payOrderVO == null) {
		// logger.error("数据库订单缺失，payOrderId:{},merchantCode:{}", notifyContent.getPayOrderId(), payOrderIdVO.getMerchantCode());
		// return notifyContent.getRspContent();
		// }
		//
		// if (payOrderVO.isClose()) { // 如果订单已经结束，则不做任何操作
		// logger.info("订单：{}已经关闭，不做任何操作", notifyContent.getPayOrderId());
		// return notifyContent.getRspContent();
		// }
		//
		// // 3.订单未关闭,但已经支付成功
		// if (payOrderVO.getStep(PayStepEnum.STEP3_PAY_RESULT) != null) {
		// logger.info("订单：{}结果响应为成功，不做任何操作", notifyContent.getPayOrderId());
		// return notifyContent.getRspContent();
		// }
		//
		// YesNoEnum isClose = YesNoEnum.YES;
		// if (PayRspCodeEnum.SUCCESS.isValue(notifyContent.getResultCode())) { // 异步回调成功，且支付状态为成功,
		// isClose = YesNoEnum.NO;
		// }

		// 写入日志
		// payService.savePayOrderLog(payOrderVO, PayStepEnum.STEP3_PAY_RESULT, payOrderVO.getPayOrderExtPO().getPayNotifyUrl(),
		// notifyContent.getReqContent(), notifyContent.getRspContent(), notifyContent.getSerialNo(), notifyContent.getResultCode(),
		// notifyContent.getResultMsg(), isClose, null);
		//
		// // 验证实名信息，更新用户实名信息
		// saveUserAuth(payOrderVO);
		//
		// // 插入异步通知任务
		// saveNotifyTask(notifyContent, payOrderVO);
		String rsp = payService.processResultNotify(paymentCode, notifyContent);

		if (PaymentEnum.QUICK_PAY.isValue(paymentCode)) {
			response.getWriter().write(rsp);
			return null;
		}

		return payService.processResultNotify(paymentCode, notifyContent);
	}
}
